@page
@inject Microsoft.AspNetCore.Mvc.Infrastructure.IActionDescriptorCollectionProvider Actions
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Routing Tables</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Routing Table</h1>

<p>
   Interesting facts:
   <ul>
       <li>
           MVC Controllers are processed first, whether it is via conventional routing or attribute routing
       </li>
       <li>
           Razor pages added last
       </li>
   </ul> 
</p>

<h2>Links in the routing table</h2>
<ul>
    @foreach (var r in Routes)
    {
        <li><a href="@r.url">@r.type: @r.url</a></li>
    }
</ul>

</body>
</html>


@functions{
    public List<(string type, string url)> Routes = new List<(string, string)>();

    public void OnGet()
    {
        foreach (var d in Actions.ActionDescriptors.Items)
        {
            if (d.AttributeRouteInfo != null)
            {
                string rr = string.Empty;
                var template = $"{d.AttributeRouteInfo?.Template}";

                if (template.StartsWith('/'))
                    rr = template;
                else
                    rr = $"/{template}";

                Routes.Add(("Attribute Routing", rr));
            }
            else
            {
                string routeValues = string.Empty;

                if (d.RouteValues.ContainsKey("area"))
                    routeValues = $"/{d.RouteValues["area"]}/{d.RouteValues["controller"]}/{d.RouteValues["action"]}";
                else
                    routeValues = $"/{d.RouteValues["controller"]}/{d.RouteValues["action"]}";

                if (!string.IsNullOrWhiteSpace(routeValues))
                    Routes.Add(("Conventional", routeValues));
            }
        }
    }
}